home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src-glut / glut_init.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  10KB  |  352 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11.  
  12. #if !defined(_WIN32)
  13. #include <X11/Xlib.h>
  14. #include <X11/Xatom.h>
  15. #endif
  16.  
  17. /* SGI optimization introduced in IRIX 6.3 to avoid X server
  18.    round trips for interning common X atoms. */
  19. #if defined(_SGI_EXTRA_PREDEFINES) && !defined(NO_FAST_ATOMS)
  20. #include <X11/SGIFastAtom.h>
  21. #else
  22. #define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
  23. #endif
  24.  
  25. #include "glutint.h"
  26.  
  27. /* GLUT inter-file variables */
  28. /* *INDENT-OFF* */
  29. char *__glutProgramName = NULL;
  30. int __glutArgc = 0;
  31. char **__glutArgv = NULL;
  32. char *__glutGeometry = NULL;
  33. Display *__glutDisplay = NULL;
  34. int __glutScreen;
  35. Window __glutRoot;
  36. int __glutScreenHeight;
  37. int __glutScreenWidth;
  38. GLboolean __glutIconic = GL_FALSE;
  39. GLboolean __glutDebug = GL_FALSE;
  40. unsigned int __glutDisplayMode =
  41.   GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH;
  42. char *__glutDisplayString = NULL;
  43. int __glutConnectionFD;
  44. XSizeHints __glutSizeHints = {0};
  45. int __glutInitWidth = 300, __glutInitHeight = 300;
  46. int __glutInitX = -1, __glutInitY = -1;
  47. GLboolean __glutForceDirect = GL_FALSE,
  48.   __glutTryDirect = GL_TRUE;
  49. Atom __glutWMDeleteWindow;
  50. /* *INDENT-ON* */
  51.  
  52. static Bool synchronize = False;
  53.  
  54. #if defined(_WIN32)
  55.  
  56. #ifdef __BORLANDC__
  57. #include <float.h>  /* For masking floating point exceptions. */
  58. #endif
  59.  
  60. void
  61. __glutOpenWin32Connection(char* display)
  62. {
  63.   static char *classname;
  64.   WNDCLASS  wc;
  65.   HINSTANCE hInstance = GetModuleHandle(NULL);
  66.   
  67.   /* Make sure we register the window only once. */
  68.   if(classname)
  69.     return;
  70.  
  71. #ifdef __BORLANDC__
  72.   /* Under certain conditions (e.g. while rendering solid surfaces with
  73.      lighting enabled) Microsoft OpenGL libraries cause some illegal
  74.      operations like floating point overflow or division by zero. The
  75.      default behaviour of Microsoft compilers is to mask (ignore)
  76.      floating point exceptions, while Borland compilers do not.  The
  77.      following function of Borland RTL allows to mask exceptions.
  78.      Advice from Pier Giorgio Esposito (mc2172@mclink.it). */
  79.   _control87(MCW_EM,MCW_EM);
  80. #endif
  81.  
  82.   classname = "GLUT";
  83.  
  84.   /* Clear (important!) and then fill in the window class structure. */
  85.   memset(&wc, 0, sizeof(WNDCLASS));
  86.   wc.style         = CS_OWNDC;
  87.   wc.lpfnWndProc   = (WNDPROC)__glutWindowProc;
  88.   wc.hInstance     = hInstance;
  89.   wc.hIcon         = LoadIcon(hInstance, "GLUT_ICON");
  90.   wc.hCursor       = LoadCursor(hInstance, IDC_ARROW);
  91.   wc.hbrBackground = NULL;
  92.   wc.lpszMenuName  = NULL;
  93.   wc.lpszClassName = classname;
  94.  
  95.   /* Fill in a default icon if one isn't specified as a resource. */
  96.   if(!wc.hIcon)
  97.     wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  98.   
  99.   if(!RegisterClass(&wc)) {
  100.     __glutFatalError("RegisterClass() failed:"
  101.              "Cannot register GLUT window class.");
  102.   }
  103.  
  104.   __glutScreenWidth     = GetSystemMetrics(SM_CXSCREEN);
  105.   __glutScreenHeight    = GetSystemMetrics(SM_CYSCREEN);
  106.  
  107.   /* Set the root window to NULL because windows creates a top-level
  108.      window when the parent is NULL.  X creates a top-level window
  109.      when the parent is the root window. */
  110.   __glutRoot            = NULL;
  111.  
  112.   /* Set the display to 1 -- we shouldn't be using this anywhere
  113.      (except as an argument to X calls). */
  114.   __glutDisplay         = (Display*)1;
  115.  
  116.   /* There isn't any concept of multiple screens in Win32, therefore,
  117.      we don't need to keep track of the screen we're on... it's always
  118.      the same one. */
  119.   __glutScreen          = 0;
  120. }
  121. #else /* !_WIN32 */
  122. void
  123. __glutOpenXConnection(char *display)
  124. {
  125.   int errorBase, eventBase;
  126.  
  127.   __glutDisplay = XOpenDisplay(display);
  128.   if (!__glutDisplay)
  129.     __glutFatalError("could not open display: %s",
  130.       XDisplayName(display));
  131.   if (synchronize)
  132.     XSynchronize(__glutDisplay, True);
  133.   if (!glXQueryExtension(__glutDisplay, &errorBase, &eventBase))
  134.     __glutFatalError(
  135.       "OpenGL GLX extension not supported by display: %s",
  136.       XDisplayName(display));
  137.   __glutScreen = DefaultScreen(__glutDisplay);
  138.   __glutRoot = RootWindow(__glutDisplay, __glutScreen);
  139.   __glutScreenWidth = DisplayWidth(__glutDisplay, __glutScreen);
  140.   __glutScreenHeight = DisplayHeight(__glutDisplay,
  141.     __glutScreen);
  142.   __glutConnectionFD = ConnectionNumber(__glutDisplay);
  143.   __glutWMDeleteWindow = XSGIFastInternAtom(__glutDisplay,
  144.     "WM_DELETE_WINDOW", SGI_XA_WM_DELETE_WINDOW, False);
  145. }
  146. #endif /* _WIN32 */
  147.  
  148. void
  149. __glutInitTime(struct timeval *beginning)
  150. {
  151.   static int beenhere = 0;
  152.   static struct timeval genesis;
  153.  
  154.   if (!beenhere) {
  155.     GETTIMEOFDAY(&genesis);
  156.     beenhere = 1;
  157.   }
  158.   *beginning = genesis;
  159. }
  160.  
  161. static void
  162. removeArgs(int *argcp, char **argv, int numToRemove)
  163. {
  164.   int i, j;
  165.  
  166.   for (i = 0, j = numToRemove; argv[j]; i++, j++) {
  167.     argv[i] = argv[j];
  168.   }
  169.   argv[i] = NULL;
  170.   *argcp -= numToRemove;
  171. }
  172.  
  173. void APIENTRY 
  174. glutInit(int *argcp, char **argv)
  175. {
  176.   char *display = NULL;
  177.   char *str, *geometry = NULL;
  178.   struct timeval unused;
  179.   int i;
  180.  
  181.   if (__glutDisplay) {
  182.     __glutWarning("glutInit being called a second time.");
  183.     return;
  184.   }
  185.   /* Determine temporary program name. */
  186.   str = strrchr(argv[0], '/');
  187.   if (str == NULL) {
  188.     __glutProgramName = argv[0];
  189.   } else {
  190.     __glutProgramName = str + 1;
  191.   }
  192.  
  193.   /* Make private copy of command line arguments. */
  194.   __glutArgc = *argcp;
  195.   __glutArgv = (char **) malloc(__glutArgc * sizeof(char *));
  196.   if (!__glutArgv)
  197.     __glutFatalError("out of memory.");
  198.   for (i = 0; i < __glutArgc; i++) {
  199.     __glutArgv[i] = __glutStrdup(argv[i]);
  200.     if (!__glutArgv[i])
  201.       __glutFatalError("out of memory.");
  202.   }
  203.  
  204.   /* determine permanent program name */
  205.   str = strrchr(__glutArgv[0], '/');
  206.   if (str == NULL) {
  207.     __glutProgramName = __glutArgv[0];
  208.   } else {
  209.     __glutProgramName = str + 1;
  210.   }
  211.  
  212.   /* parse arguments for standard options */
  213.   for (i = 1; i < __glutArgc; i++) {
  214.     if (!strcmp(__glutArgv[i], "-display")) {
  215. #if defined(_WIN32)
  216.       __glutWarning("-display option invalid for win32 glut.");
  217. #endif
  218.       if (++i >= __glutArgc) {
  219.         __glutFatalError(
  220.           "follow -display option with X display name.");
  221.       }
  222.       display = __glutArgv[i];
  223.       removeArgs(argcp, &argv[1], 2);
  224.     } else if (!strcmp(__glutArgv[i], "-geometry")) {
  225.       if (++i >= __glutArgc) {
  226.         __glutFatalError(
  227.           "follow -geometry option with geometry parameter.");
  228.       }
  229.       geometry = __glutArgv[i];
  230.       removeArgs(argcp, &argv[1], 2);
  231.     } else if (!strcmp(__glutArgv[i], "-direct")) {
  232. #if defined(_WIN32)
  233.       __glutWarning("-direct option invalid for win32 glut.");
  234. #endif
  235.       if (!__glutTryDirect)
  236.         __glutFatalError(
  237.           "cannot force both direct and indirect rendering.");
  238.       __glutForceDirect = GL_TRUE;
  239.       removeArgs(argcp, &argv[1], 1);
  240.     } else if (!strcmp(__glutArgv[i], "-indirect")) {
  241. #if defined(_WIN32)
  242.       __glutWarning("-indirect option invalid for win32 glut.");
  243. #endif
  244.       if (__glutForceDirect)
  245.         __glutFatalError(
  246.           "cannot force both direct and indirect rendering.");
  247.       __glutTryDirect = GL_FALSE;
  248.       removeArgs(argcp, &argv[1], 1);
  249.     } else if (!strcmp(__glutArgv[i], "-iconic")) {
  250.       __glutIconic = GL_TRUE;
  251.       removeArgs(argcp, &argv[1], 1);
  252.     } else if (!strcmp(__glutArgv[i], "-gldebug")) {
  253.       __glutDebug = GL_TRUE;
  254.       removeArgs(argcp, &argv[1], 1);
  255.     } else if (!strcmp(__glutArgv[i], "-sync")) {
  256. #if defined(_WIN32)
  257.       __glutWarning("-indirect option invalid for win32 glut.");
  258. #endif
  259.       synchronize = GL_TRUE;
  260.       removeArgs(argcp, &argv[1], 1);
  261.     } else {
  262.       /* Once unknown option encountered, stop command line
  263.          processing. */
  264.       break;
  265.     }
  266.   }
  267. #if defined(_WIN32)
  268.   __glutOpenWin32Connection(display);
  269. #else
  270.   __glutOpenXConnection(display);
  271. #endif
  272.   if (geometry) {
  273.     int flags, x, y, width, height;
  274.  
  275.     /* Fix bogus "{width|height} may be used before set"
  276.        warning */
  277.     width = 0;
  278.     height = 0;
  279.  
  280.     flags = XParseGeometry(geometry, &x, &y,
  281.       (unsigned int *) &width, (unsigned int *) &height);
  282.     if (WidthValue & flags) {
  283.       /* Careful because X does not allow zero or negative
  284.          width windows */
  285.       if (width > 0)
  286.         __glutInitWidth = width;
  287.     }
  288.     if (HeightValue & flags) {
  289.       /* Careful because X does not allow zero or negative
  290.          height windows */
  291.       if (height > 0)
  292.         __glutInitHeight = height;
  293.     }
  294.     glutInitWindowSize(__glutInitWidth, __glutInitHeight);
  295.     if (XValue & flags) {
  296.       if (XNegative & flags)
  297.         x = DisplayWidth(__glutDisplay, __glutScreen) +
  298.           x - __glutSizeHints.width;
  299.       /* Play safe: reject negative X locations */
  300.       if (x >= 0)
  301.         __glutInitX = x;
  302.     }
  303.     if (YValue & flags) {
  304.       if (YNegative & flags)
  305.         y = DisplayHeight(__glutDisplay, __glutScreen) +
  306.           y - __glutSizeHints.height;
  307.       /* Play safe: reject negative Y locations */
  308.       if (y >= 0)
  309.         __glutInitY = y;
  310.     }
  311.     glutInitWindowPosition(__glutInitX, __glutInitY);
  312.   }
  313.   __glutInitTime(&unused);
  314. }
  315.  
  316. /* CENTRY */
  317. void APIENTRY 
  318. glutInitWindowPosition(int x, int y)
  319. {
  320.   __glutInitX = x;
  321.   __glutInitY = y;
  322.   if (x >= 0 && y >= 0) {
  323.     __glutSizeHints.x = x;
  324.     __glutSizeHints.y = y;
  325.     __glutSizeHints.flags |= USPosition;
  326.   } else {
  327.     __glutSizeHints.flags &= ~USPosition;
  328.   }
  329. }
  330.  
  331. void APIENTRY 
  332. glutInitWindowSize(int width, int height)
  333. {
  334.   __glutInitWidth = width;
  335.   __glutInitHeight = height;
  336.   if (width > 0 && height > 0) {
  337.     __glutSizeHints.width = width;
  338.     __glutSizeHints.height = height;
  339.     __glutSizeHints.flags |= USSize;
  340.   } else {
  341.     __glutSizeHints.flags &= ~USSize;
  342.   }
  343. }
  344.  
  345. void APIENTRY 
  346. glutInitDisplayMode(unsigned int mask)
  347. {
  348.   __glutDisplayMode = mask;
  349. }
  350.  
  351. /* ENDCENTRY */
  352.